package com.its.common.net.server.channel;

import cn.hutool.core.lang.Singleton;
import io.netty.channel.Channel;
import io.netty.channel.ChannelId;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author xiaxp
 * @Date 9/1/22 17:14
 * @Description
 */
@Slf4j
public class DefaultNetChannelListener implements NetChannelListener {

    private ChannelManager channelManager;

    private DefaultNetChannelListener() {
        channelManager = DefaultChannelManager.getInstance();
    }

    public static NetChannelListener getInstance() {
        return Singleton.get(DefaultNetChannelListener.class);
    }

    @Override
    public void channelActive(Channel channel) {
        channelManager.addChannel(channel);
        log.info("成功添加新Channel, Channel={}", channel);
    }

    @Override
    public void channelInactive(Channel channel) {
        ChannelId channelId = channel.id();
        channelManager.removeChannel(channelId);
        log.info("成功移除Channel, Channel={}", channel);
    }
}
